package com.wx.tnsperson.controller;

import com.wx.common.api.CommonResult;
import com.wx.common.api.ResultCode;
import com.wx.dto.PageDto;
import com.wx.tnsperson.model.ProResumeModel;
import com.wx.tnsperson.service.impl.ProResumeServiceImpl;
import com.wx.tnsperson.vo.ResumeVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Classname ProResumeController
 * @Description TODO
 * @Date 2020/6/24 10:23
 * @Created by llh
 */

@Slf4j
@RestController
@Api(tags = "个人简历")
@RequestMapping("/resume")
public class ProResumeController {

    @Autowired
    private ProResumeServiceImpl proResumeService;


    //查询所有
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有的简历", notes = "查询所有的简历列表中的信息")
    public CommonResult findAll () {
        List<ResumeVo> all = null;
        try {
            all = proResumeService.findAll ( );
            if (CollectionUtils.isEmpty ( all )) {
                return CommonResult.failed ( "查询失败" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( "服务器处理异常" );
        }
        return CommonResult.success ( all,"查询成功" );
    }

    //根据id查询
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询简历", notes = "输入id 查询简历信息")
    @ApiImplicitParam(name = "id", value = "字段id", required = true, paramType = "query", dataType = "Long")
    public ResponseEntity findById ( Integer id ) {
        try {
            if (id != null && id == 0) {
                return ResponseEntity.badRequest ( ).build ( );
            } else {
                ResumeVo result = proResumeService.findById ( id );
                return ResponseEntity.ok ( result );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return ResponseEntity.status ( 500 ).build ( );
        }
    }

    // 根据id删除简历
    @RequestMapping(value = "/delResume", method = RequestMethod.DELETE)
    @ApiOperation(value = "根据id删除简历")
    @ApiImplicitParam(name = "id", value = "表字段id", required = true, paramType = "query", dataType = "Long")
    public CommonResult delResume ( Integer id ) {
        try {
            if (id != null && id == 0) {
                return CommonResult.validateFailed ( "请求参数不正确" );
            }
            Integer result = proResumeService.delResume ( id );
            if (result <= 0)
                return CommonResult.failed ( ResultCode.NOT_FOUNT );
            return CommonResult.success ( result,"删除成功" );
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    //简历上传头像
    @RequestMapping(value = "/updateFile", method = RequestMethod.POST)
    @ApiOperation(value = "简历上传头像")
    public CommonResult updateFile ( ProResumeModel proResumeModel,
                                     @RequestParam(value = "headPh", required = false) MultipartFile headPh,
                                     HttpServletRequest request ) {
        try {
            proResumeService.updateFile ( proResumeModel,headPh,request );
            ResumeVo byId = proResumeService.findById ( proResumeModel.getId ( ) );
            String url=byId.getHeadPh ();
            return CommonResult.success ( url,"上传成功" );
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( "上传失败！" );
        }
    }

    //简历添加
    @RequestMapping(value = "/updateResume", method = RequestMethod.POST)
    @ApiOperation(value = "简历添加")
    public CommonResult updateResume ( ResumeVo resumeVo ) {
        try {
            Integer result = proResumeService.updateResume ( resumeVo );
            if (result <= 0) {
                return CommonResult.failed ( "添加失败" );
            } else {
                return CommonResult.success ( resumeVo,"添加" + result + "条简历数据成功！" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

}
